package com.cdut.logistics.model.dao.datacentre;

import java.util.Date;

import com.cdut.logistics.model.entity.datacentre.BaseUser;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.cdut.logistics.model.entity.datacentre.BaseUser;

/**  
 * @ClassName: User 
 * @Description: 操作数据库 
 * @author wxt.xia
 * @date 2018年1月26日 上午9:13:52    
 */
public class User extends BaseUser<User>{
	private static final long serialVersionUID = 1L;
	public static  User dao = new User();
	public static String  table  = "user";
	public String baseSql = " and is_use = 1";
	public String sql1 = "SELECT "+
			"user.id,"+
			"user.`name`,"+
			"user.identity,"+
			"school.`name` as 's_name',"+
			"academy.`name` as 'a_name',"+
			"major.`name` as 'm_name',"+
			"(CONCAT(area.`name`,'-',dorm.`name`)) as 'dorm_name',"+
			"user.phone,"+
			"sys_dict.description as 'sex' "+
			"from `user` JOIN school ON user.school_id = school.id AND school.is_use=1 AND user.name like ? AND user.is_use =1 "+
			"JOIN academy ON `user`.academy_id = academy.id AND academy.is_use=1 "+
			"JOIN dorm ON user.dorm_id = dorm.id AND dorm.is_use=1 "+
			"JOIN major ON user.major_id = major_id AND major.is_use=1 "+
			"JOIN	sys_dict	ON sys_dict.type = \"sex\" AND `user`.sex = sys_dict.`value` AND sys_dict.is_use=1 " +
			"JOIN area ON dorm.area_id = area.id";
	public String sql2 = "SELECT "+
			"user.id,"+
			"user.`name`,"+
			"user.identity,"+
			"school.`name` as 's_name',"+
			"academy.`name` as 'a_name',"+
			"major.`name` as 'm_name',"+
			"(CONCAT(area.`name`,'-',dorm.`name`)) as 'dorm_name',"+
			"user.phone,"+
			"sys_dict.description as 'sex' "+
			"from `user` JOIN school ON user.school_id = school.id AND school.is_use=1 AND user.id =? AND user.is_use =1 "+
			"JOIN academy ON `user`.academy_id = academy.id AND academy.is_use=1 "+
			"JOIN dorm ON user.dorm_id = dorm.id AND dorm.is_use=1 "+
			"JOIN major ON user.major_id = major_id AND major.is_use=1 "+
			"JOIN	sys_dict	ON sys_dict.type = \"sex\" AND `user`.sex = sys_dict.`value` AND sys_dict.is_use=1 "+
			"JOIN area ON dorm.area_id = area.id";
	public String sql4 = "select * from user where student_id like ?"+ baseSql;
	public String sql3 = "UPDATE user set is_use =?, updated=? where id =?"+ baseSql;
	/**  
	 * @Title: save  
	 * @Description: 新增用户
	 * @author wxt.xia
	 * @param user
	 * @throws  
	 */
	public void save(User user) {
		user.set("created", new Date());
		user.set("updated", new Date());
		user.set("is_use", 1);
		user.set("active", 1);
		user.save();
	}

	/**  
	 * @Title: remove  
	 * @Description: 删除用户
	 * @author wxt.xia
	 * @param id
	 * @throws  
	 */
	public void remove(int id) {
		Db.update(sql3,0,new Date(),id);
	}

	/**  
	 * @Title: queryById  
	 * @Description: 根据用户id查询字典值记录
	 * @author wxt.xia
	 * @param id
	 * @return
	 * @throws  
	 */
	public User queryById(int id) {
		return findById(id);
	}
	public boolean validateIfLogin(String access_token){
		String sql="select * from user WHERE access_token=?";
		boolean result=true;
		System.out.println(Db.findFirst(sql,access_token));
		if(Db.findFirst(sql,access_token)!=null){
		}
		else{
			result=false;
		}
		System.out.println("result : ============="+result);
		return result;
	}
	public boolean validateIfAuthority(String access_token,String url){
		String sql="SELECT url FROM authority JOIN `user` JOIN role ON `user`.role_id=role.id and role.Authority_id=authority.id WHERE `user`.access_token=?";
		String sql2="SELECT role_id from user where access_token=?";
		boolean result=false;
		Record re2=Db.findFirst(sql2,access_token);
		if(re2.getInt("role_id")==1)
		{
			result=true;
			return result;
		}
		if(Db.findFirst(sql,access_token)==null)
		{
			return result;
		}
		else
		{
			Record re=Db.findFirst(sql,access_token);
			String [] urlnameList = new String[1];
			try {
				urlnameList = re.getStr("url").split("\\*");
			} catch (Exception e) {
				urlnameList[0]=re.getStr("url");
			}

			for(int i=0;i<urlnameList.length;i++){
				System.out.println("funcname=========="+urlnameList[i]);
				if(url.equals(urlnameList[i])){
					result=true;
				}
			}
		}
		return result;
	}
	//
	//	public boolean validateIfRepairMem(String access_token,String funcname) {
	//		String sql="SELECT url FROM authority JOIN `user` JOIN role ON `user`.role_id=role.id and role.Authority_id=authority.id WHERE `user`.access_token=?";
	//		boolean result=false;
	//		String sql1="select role_id from user WHERE access_token=?";
	//		if(Db.findFirst(sql1,access_token).getInt("role_id")==1)
	//		{
	//			result=true;
	//			return result;
	//		}
	//		if(Db.findFirst(sql,access_token)==null)
	//		{
	//			return result;
	//		}
	//		else
	//		{
	//			Record re=Db.findFirst(sql,access_token);
	//			String[] urlname = re.getStr("url").split("/");
	//		for(int i=0;i<urlname.length;i++){
	//			System.out.println("funcname=========="+urlname[i]);
	//			if(funcname.equals(urlname[i])){
	//				result=true;
	//			}
	//		}
	//		}
	//		return result;
	//	}
	//	public boolean validateIfRepairLeader(String access_token,String funcname){
	//		String sql="SELECT url FROM authority JOIN `user` JOIN role ON `user`.role_id=role.id and role.Authority_id=authority.id WHERE `user`.access_token=?";
	//		boolean result=false;
	//		String sql1="select role_id from user WHERE access_token=?";
	//		if(Db.findFirst(sql1,access_token).getInt("role_id")==1)
	//		{
	//			result=true;
	//			return result;
	//		}
	//		else if(Db.findFirst(sql,access_token)==null)
	//		{
	//			return result;
	//		}
	//		else
	//		{
	//			Record re=Db.findFirst(sql,access_token);
	//			String[] urlname = re.getStr("url").split("/");
	//		for(int i=0;i<urlname.length;i++){
	//			System.out.println("funcname=========="+urlname[i]);
	//			if(funcname.equals(urlname[i])){
	//				result=true;
	//			}
	//		}
	//		}
	//		return result;
	//	}
	//	public boolean validateIfOperationMem(String access_token,String funcname){
	//		String sql="SELECT url FROM authority JOIN `user` JOIN role ON `user`.role_id=role.id and role.Authority_id=authority.id WHERE `user`.access_token=?";
	//		boolean result=false;
	//		String sql1="select role_id from user WHERE access_token=?";
	//		if(Db.findFirst(sql1,access_token).getInt("role_id")==1)
	//		{
	//			result=true;
	//			return result;
	//		}
	//		if(Db.findFirst(sql,access_token)==null)
	//		{
	//			return result;
	//		}
	//		else
	//		{
	//			Record re=Db.findFirst(sql,access_token);
	//			String[] urlname = re.getStr("url").split("/");
	//		for(int i=0;i<urlname.length;i++){
	//			System.out.println("funcname=========="+urlname[i]);
	//			if(funcname.equals(urlname[i])){
	//				result=true;
	//			}
	//		}
	//		}
	//		return result;
	//	}
}
